package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    private static final Logger log = LoggerFactory.getLogger(CategoryController.class);
    @Autowired
    CategoryService categoryService;
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();


    }
    @PutMapping()
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类：{}", categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();


    }
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("修改分类状态：{}", status);
        categoryService.startOrStop(status, id);
        return Result.success();


    }
    @GetMapping("/page")
    public Result<PageResult> Selecet(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询：{},{}",categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
       return Result.success( categoryService.Select(categoryPageQueryDTO));

    }

    @DeleteMapping()
    public Result Delete(String id){
        log.info("删除分类,id={}", id);
        categoryService.Delete(id);
        return Result.success();
    }
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        log.info("查询分类");

        List<Category> list = categoryService.GetTypeByType(type);
        return Result.success(list);
    }




}
